package com.ornate.platform.user.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.ornate.core.provider.ActionSupportProvider;
import com.ornate.platform.user.service.IUserService;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class UserAction extends ActionSupportProvider {

	private static final long serialVersionUID = 9012256390225787575L;

	public UserAction() {
		System.out.println("\n\n\n" + UserAction.class + " Inited...\n\n\n");
	}

	private IUserService userService;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String count() {
		return "index";
	}

	public String query() throws Exception {
		Configuration fcfg = new Configuration();
		File file = new File(UserAction.class.getClassLoader().getResource("").getPath() + "com/ornate/template/template");
		fcfg.setDirectoryForTemplateLoading(file);
		fcfg.setObjectWrapper(new DefaultObjectWrapper());

		Template template = fcfg.getTemplate("dao/interface.ftl");

		Map<String, Object> data = new HashMap<String, Object>(), clz = new HashMap<String, Object>(), method = new HashMap<String, Object>();

		clz.put("param", "param");
		clz.put("package", "com.ornate.platform.user.dao");
		clz.put("name", "IUserDAO");

		method.put("query", "querys");
		method.put("count", "counts");
		method.put("insert", "inserts");
		method.put("update", "updates");
		method.put("delete", "deletes");

		clz.put("method", method);

		//		clz.put("imports", new String[]{"query","count","insert"});

		data.put("class", clz);

		FileOutputStream fos = new FileOutputStream("F:/x.java");

		Writer out = new OutputStreamWriter(fos, "UTF-8");

		template.process(data, out);
		out.flush();
		out.close();
		return null;
	}

	public String test() {
		setJSONObject(userService.query());
		return JSON_OBJECT;
	}

	public static void main(String[] args) throws Exception {
		char[] chars = { 'c', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'z' };
		int length = 4, count = 500, max = chars.length;
		Random random = new Random();
		String z = "";

		Set<String> set = new HashSet<String>();

		for (int i = 0; i < count; i++) {
			for (int j = 0; j < length; j++) {
				z += chars[random.nextInt(max)];
			}
			set.add(z);
			z = "";
			//			System.out.println(z + "\t\t\t\t" + StringUtils.lowerCase(z) + "\t\t\t\t" + StringUtils.capitalize(z) + "\t\t\t\t" + StringUtils.upperCase(z));
			//			z = "";
		}

		System.out.println(set.size());

		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			z = (String) iterator.next();
			System.out.println(StringUtils.lowerCase(z) + "\n" + StringUtils.capitalize(z) + "\n" + StringUtils.upperCase(z) + "\n------------------------------------------------------");
		}

	}

}
